<?php

declare(strict_types=1);

namespace app\admin\service\file;

use Dengje\Pxy\BaseService;

class FileService extends BaseService
{
    protected $model = null;


    public function __construct()
    {

        $this->model = new \app\common\model\file\FileModel();

    }

    public function list()
    {
        $where = [];
        if (request()->get('media_type')) {
            $mediaType = request()->get('media_type');
            if ($mediaType == 'image') {

                // 定义图片MIME类型数组
                $mimeTypes = [
                    'image/jpeg',
                    'image/jpg',
                    'image/png',
                    'image/gif',
                    'image/bmp',
                    'image/webp',
                    'image/tiff',
                    'image/x-tiff',
                    'image/x-icon',
                    'image/vnd.microsoft.icon',
                    'image/x-adobe-dng',
                    // 可以继续添加其他图片MIME类型
                ];
                $where[] = ['mimetype', 'in', $mimeTypes];
            } elseif ($mediaType == 'video') {
                $mimeTypes = [
                    'video/mp4',
                    'video/x-msvideo',
                    'video/x-ms-wmv',
                    'video/quicktime',
                    'video/3gpp',
                    'video/3gpp2',
                    'video/x-flv',
                    'video/webm',
                    'video/x-matroska',
                    'video/ogg',
                    'application/octet-stream',
                    // 可以继续添加其他视频MIME类型
                ];
                $where[] = ['mimetype', 'in', $mimeTypes];
            } elseif ($mediaType == 'file') {
                $mimeTypes = [
                    'text/plain',
                    'text/html',
                    'text/css',
                    'application/javascript',
                    'application/xml',
                    'application/pdf',
                    'audio/mpeg'
                ];
                $where[] = ['mimetype', 'in', $mimeTypes];
            }
        }
        ;
        if (request()->get('group_id')) {
            $where['group_id'] = request()->get('group_id');
        }
        $list = $this->model->where($where)->order(['id' => 'desc'])->paginate(request()->get('page_size/d', 10));
        return $list;
    }

    public function create($data)
    {
        return $this->model->create($data);
    }

    public function update($id, $data)
    {
        $data = $this->preExcludeFields($data);
        return $this->model->where('id', $id)->update($data);
    }

    public function detail($id)
    {
        return $this->model->where('id', $id)->find();
    }

    public function delete($id)
    {
        return $this->model->where('id', $id)->delete();
    }
}